package com.wyw.learning.thread.threadlocal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 用加锁的方式解决线程安全问题
 *  但是高并发情况下是一个个排队的形式，其实也不是太好
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * @date 2021/7/6   10:24 下午
 * <p>
 * Update History:
 * Author        Time            Content
 */
public class ThreadLocalNormalUsage04 {
    public static ExecutorService threadPool = Executors.newFixedThreadPool(10);
    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    String date = new ThreadLocalNormalUsage03().date(finalI);
                    System.out.println(date);
                }
            });
        }
        threadPool.shutdown();
    }

    public String date(int seconds) {
        //参数的单位是毫秒，从1970.1.1 00:00:00 GMT计时
        Date date = new Date(1000 * seconds);
        String result = null;
        synchronized (ThreadLocalNormalUsage04.class){//加类锁
            result = dateFormat.format(date);
        }
        return result;
    }
}
